import axios from "axios"
import {ElMessage} from "element-plus"
import {useUserStore} from "../store/users.ts";


//前缀地址
// const baseURL = 'http://localhost:8101'
const baseURL = 'http://182.92.211.184:8101'


const request = axios.create({
    baseURL: baseURL,
})

// 请求拦截器
// 请求头添加token
request.interceptors.request.use((config) => {
    config.headers['token'] = useUserStore().user.token
    return config
}, (error) => {
    Promise.reject(error)
})


// 响应拦截器
// 处理错误信息
request.interceptors.response.use((response) => {
    const res = response.data
    if (res.code === 200) {
        ElMessage({
            message: res.msg, type: 'success'
        })
    } else {
        ElMessage({
            message: res.msg, type: 'error'
        })
    }
    return res
}, (error) => {
    ElMessage({
        message: '网络连接超时，请稍后再试', type: 'error'
    })
    console.log(error)
})

export default request